//
//  ViewController.m
//  UIKIit_TomCat
//
//  Created by 李竞 on 16/1/25.
//  Copyright © 2016年 lionjing. All rights reserved.
//

/**图片的格式: 
 JPG : 压缩比比较高,同化成那个用于照片,网页
        属于有损压缩,解压缩时对CPU消耗大,意味着费电,慢
 PNG : 压缩比较高, 解压缩消耗小,省电,快,苹果推荐 .
 */
#import "ViewController.h"

@interface ViewController ()

@property (weak, nonatomic) IBOutlet UIImageView *imgTom;
@property (nonatomic , strong) NSDictionary *dict_IMG ;
@end

@implementation ViewController
-(NSDictionary *)dict_IMG{
    if (!_dict_IMG) {
        NSString *path = [[NSBundle mainBundle] pathForResource:@"tom.plist" ofType:nil];
        _dict_IMG = [NSDictionary dictionaryWithContentsOfFile:path];
    }
    return _dict_IMG ;
}

- (void)viewDidLoad {
    [super viewDidLoad];
}
//敲锣
- (IBAction)cymbal_Clicked:(UIButton *)sender {
    [ self IMG_Animation_Name:@"cymbal" with_Count:[self.dict_IMG[@"cymbal"] intValue]];
}
//喝奶:
- (IBAction)drink_Clicked:(UIButton *)sender {
    [ self IMG_Animation_Name:@"drink" with_Count:[self.dict_IMG[@"drink"] intValue]];
}
//吃麻雀:
- (IBAction)eat_Clicked:(UIButton *)sender {
    [ self IMG_Animation_Name:@"eat" with_Count:[self.dict_IMG[@"eat"] intValue]];
}
//废气排放:
- (IBAction)fart_Clicked:(UIButton *)sender {
    [ self IMG_Animation_Name:@"fart" with_Count:[self.dict_IMG[@"fart"] intValue]];
}
//痛:
- (IBAction)pie_Clicked:(UIButton *)sender {
    [ self IMG_Animation_Name:@"pie" with_Count:[self.dict_IMG[@"pie"] intValue]];
}
//熊掌:
- (IBAction)scratch:(UIButton *)sender {
    [ self IMG_Animation_Name:@"scratch" with_Count:[self.dict_IMG[@"scratch"] intValue]];
}

/**
 imageNamed: 是系统推荐使用的方法, 但是图像实例化之后的释放由系统负责,如果想要自己释放图片,不能使用imageNamed方法!
 */

-(void)IMG_Animation_Name:(NSString *)pre_string with_Count:(NSInteger)totalCount{
    //判断是否正在动画:
    if ([self.imgTom isAnimating]) return;
    //动画图片数组 :
    NSMutableArray *array_M = [NSMutableArray arrayWithCapacity:totalCount];

    for (int i = 0; i < totalCount; i++) {
        NSString *imgName = [NSString stringWithFormat:@"%@_%02d.jpg",pre_string, i];
        NSString *path = [[NSBundle mainBundle]pathForResource:imgName ofType:nil];
        UIImage *IMG = [UIImage imageWithContentsOfFile:path];
        [array_M addObject:IMG];
    }

    //设置动画数组 :
    self.imgTom.animationImages = array_M ;
    self.imgTom.animationRepeatCount = 1 ;
    self.imgTom.animationDuration = array_M.count / 24.0;

    //开始动画 :
    [self.imgTom startAnimating];

    //动画结束之后,清理动画数组:
    [self.imgTom performSelector:@selector(setAnimationImages:) withObject:nil afterDelay:self.imgTom.animationDuration];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
